﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Lvzhi.AdminCenter.Models;
using Lvzhi.Core;
using Lvzhi.Entities;
using Lvzhi.Provide.JsonHelper;
using Newtonsoft.Json;
using Lvzhi.AdminCenter.Extension.MvcHelper;
using Webdiyer.WebControls.Mvc;

namespace Lvzhi.AdminCenter.Controllers
{
    public class OrderController : BaseController
    {
        private UserCore ucore = new UserCore();
        private OrderCore ocore = new OrderCore();
        private ProductCore pcore = new ProductCore();
        private UserProductCore upcore = new UserProductCore();
        private TicketCore tcore = new TicketCore();
        private TicketHistoryCore thcore = new TicketHistoryCore();

        /// <summary>
        /// 保单查询
        /// </summary>
        /// <param name="id"></param>
        /// <param name="size"></param>
        /// <param name="Product"></param>
        /// <param name="no"></param>
        /// <param name="uname"></param>
        /// <param name="state"></param>
        /// <param name="savailtime"></param>
        /// <param name="eavailtime"></param>
        /// <param name="sno"></param>
        /// <param name="eno"></param>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <returns></returns>
        public ActionResult Index(int id = 1, int size = 10, string Product = "", string no = "", string uname = "", int state = -2, string savailtime = "", string eavailtime = "", int sno = 0, int eno = 0, string stime = "", string etime = "", int uid = -1)
        {
            Core_User user = GetUser();
            //默认查出所有的产品
            var plist = pcore.LoadEntities(o => o.P_State == 0).ToList();

            //下拉产品选择
            List<SelectListItem> plist1 = null;
            plist1 = plist.Select(o => new SelectListItem()
            {
                Text = o.P_Name,
                Value = o.P_Id.ToString()
            }).ToList();


            //            if (user.U_Role == 0)
            //            {
            //                // plist = pcore.LoadEntities(o=>o.P_State == 0).ToList().Select(o=>new KeyValuePair<string,string>(o.P_Id.ToString(),o.P_Name)).ToList();
            //                plist1 = plist.Select(o => new SelectListItem()
            //                {
            //                    Text = o.P_Name,
            //                    Value = o.P_Id.ToString()
            //                }).ToList();
            //            }
            //            else
            //            {
            //                //plist = upcore.LoadEntities(o=>o.UP_UId == user.U_Id).ToList().Select(o => new KeyValuePair<string, string>(o.UP_PId.ToString(), o.UP_PName)).ToList();
            //                var temp = upcore.LoadEntities(o => o.UP_UId == user.U_Id).ToList();
            //
            //
            //                plist1 = plist.Where(a => temp.Any(b => a.P_Id == b.UP_PId)).Select(o => new SelectListItem()
            //                {
            //                    Text = o.P_Name,
            //                    Value = o.P_Id.ToString()
            //                }).ToList();
            //            }

            plist1.Insert(0, new SelectListItem() { Text = "==请选择==", Value = "-1", Selected = false });
            TempData["Product"] = plist1;

            //筛选条件
            Expression<Func<Core_Order, Boolean>> expr = t => true;

            Guid pid = Guid.Empty;
            if (!string.IsNullOrWhiteSpace(Product) && Guid.TryParse(Product, out pid))
            {
                expr = expr.And(o => o.O_ProductId == pid);
            }

            if (!string.IsNullOrWhiteSpace(no))
            {
                expr = expr.And(o => o.O_PolicyNo.Contains(no));
            }
            if (!string.IsNullOrWhiteSpace(uname))
            {
                expr = expr.And(o => o.O_UserName.Contains(uname));
            }

            if (state != -2)
            {
                expr = expr.And(o => o.O_State == state);
            }
            if (sno > 0)
            {
                expr = expr.And(o => o.O_TicketNo >= sno);
            }
            if (eno > 0)
            {
                expr = expr.And(o => o.O_TicketNo <= eno);
            }

            DateTime svtime;
            if (!string.IsNullOrWhiteSpace(savailtime) && DateTime.TryParse(savailtime, out svtime))
            {
                expr = expr.And(o => o.O_STime >= svtime);
            }

            DateTime evtime;
            if (!string.IsNullOrWhiteSpace(eavailtime) && DateTime.TryParse(eavailtime, out evtime))
            {
                expr = expr.And(o => o.O_ETime <= evtime);
            }

            DateTime sstime;
            if (!string.IsNullOrWhiteSpace(stime) && DateTime.TryParse(stime, out sstime))
            {
                expr = expr.And(o => o.O_CreateTime >= sstime);
            }

            DateTime eetime;
            if (!string.IsNullOrWhiteSpace(etime) && DateTime.TryParse(etime, out eetime))
            {
                expr = expr.And(o => o.O_CreateTime <= eetime);
            }

            if (uid > 0)  //查询某一个人的
            {
                expr = expr.And(o => o.O_WorkerId == uid);
            }
            else
            {
                if (user.U_Role != 0) //不是超管查自己的
                {
                    expr = expr.And(o => o.O_WorkerId == user.U_Id);
                }
            }


            var list = ocore.LoadEntities(expr).OrderByDescending(o => o.O_CreateTime).ToPagedList(id, size);



            return View(list);
        }
        /// <summary>
        /// 新单购买
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit()
        {
            Core_User user = GetUser();

            //List<KeyValuePair<string, string>> plist = new List<KeyValuePair<string, string>>();
            //默认查出所有的产品
            var plist = pcore.LoadEntities(o => o.P_State == 0).ToList();

            //下拉产品选择
            IEnumerable<SelectListItem> plist1 = null;

            int InvoiceCount = 0;
            int InterfaceCount = 0;
            int TicketCount = 0;


            if (user.U_Role == 0)
            {
                // plist = pcore.LoadEntities(o=>o.P_State == 0).ToList().Select(o=>new KeyValuePair<string,string>(o.P_Id.ToString(),o.P_Name)).ToList();
                plist1 = plist.Select(o => new SelectListItem()
                {
                    Text = o.P_Name,
                    Value = o.P_Id.ToString() + "###" + o.P_Memo
                });
            }
            else
            {
                //plist = upcore.LoadEntities(o=>o.UP_UId == user.U_Id).ToList().Select(o => new KeyValuePair<string, string>(o.UP_PId.ToString(), o.UP_PName)).ToList();
                var temp = upcore.LoadEntities(o => o.UP_UId == user.U_Id).ToList();


                plist1 = plist.Where(a => temp.Any(b => a.P_Id == b.UP_PId)).Select(o => new SelectListItem()
                {
                    Text = o.P_Name,
                    Value = o.P_Id.ToString() + "###" + o.P_Memo
                });
            }

            TempData["ProductList"] = plist1;
            //TempData["User"] = user;
            ViewBag.InvoiceCount = user.U_InvoiceCount;
            ViewBag.InterfaceCount = user.U_InterfaceCount;
            ViewBag.TicketCount = user.U_TicketCount;
            //ViewBag.TicketCurent = user.U_


            return View();
        }
        /// <summary>
        /// 获取 单据数量
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult TicketCount(Guid id)
        {

            var user = GetUser();
            if (user.U_Role == 0) //超管
            {
                var ticket = tcore.LoadEntities(o => o.T_ProductId == id && o.T_State == 0 && o.T_Flag == 0).OrderByDescending(o => o.T_RemainCount).FirstOrDefault();

                if (ticket != null)
                {
                    return Json(new { remain = ticket.T_RemainCount, curno = ticket.T_CurrentNo }, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                //其他人，从分配的产品单据里面去获取剩余最多的单证
                var ticket = thcore.LoadEntities(o => o.H_ProductId == id && o.H_State == 0 && o.H_Flag == 0).OrderByDescending(o => o.H_RemainCount).FirstOrDefault();


                if (ticket != null)
                {
                    return Json(new { remain = ticket.H_RemainCount, curno = ticket.H_CurrentNo }, JsonRequestBehavior.AllowGet);
                }
            }


            return Json("err");
        }

        /// <summary>
        /// 投保
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Policy(PolicyViewModel obj)
        {
            var rs = new ResultDataModel();

            Guid pid = obj.ProductId;

            var pinfo = pcore.LoadEntity(o => o.P_Id == pid && o.P_State == 0);

            Core_Ticket ticket = null;
            Core_TicketHistory ticketHistory = null;

            if (pinfo != null) //产品存在
            {
                var user1 = GetUser();
                var user = ucore.LoadEntity(o => o.U_Id == user1.U_Id);

                if (user.U_Role == 0) //超管
                {
                    ticket =
                        tcore.LoadEntities(o => o.T_ProductId == pid && o.T_State == 0 && o.T_Flag == 0)
                            .OrderByDescending(o => o.T_RemainCount)
                            .FirstOrDefault();


                    if (ticket == null)
                    {
                        rs.Msg = "单证不存在";
                    }
                    else if (obj.Insured.Count > ticket.T_RemainCount)
                    {
                        rs.Msg = "单证数量不足";
                    }
                    else if (obj.Insured.Count > user.U_InvoiceCount)
                    {
                        rs.Msg = "发票次数不足";
                    }
                    else //符合要求
                    {
                        rs.Code = 0;
                        //                        //修改剩余
                        //                        ticket.T_RemainCount -= obj.Insured.Count;
                        //                        ticket.T_CurrentNo += obj.Insured.Count;
                        //                        tcore.UpdateEntity(ticket);
                        //
                        //                        var user2 = ucore.LoadEntity(o => o.U_Id == user.U_Id && o.U_State == 0);
                    }
                }
                else
                {
                    //其他人，从分配的产品单据里面去获取剩余最多的单证
                    ticketHistory =
                        thcore.LoadEntities(o => o.H_ProductId == pid && o.H_State == 0 && o.H_Flag == 0)
                            .OrderByDescending(o => o.H_RemainCount)
                            .FirstOrDefault();

                    if (ticketHistory == null)
                    {
                        rs.Msg = "单证不存在";
                    }
                    else if (obj.Insured.Count > ticketHistory.H_RemainCount)
                    {
                        rs.Msg = "单证数量不足";
                    }
                    else if (obj.Insured.Count > user.U_InvoiceCount)
                    {
                        rs.Msg = "发票次数不足";
                    }
                    else //符合要求
                    {
                        rs.Code = 0;
                        //                        //修改剩余
                        //                        ticket.T_RemainCount -= obj.Insured.Count;
                        //                        ticket.T_CurrentNo += obj.Insured.Count;
                        //                        tcore.UpdateEntity(ticket);
                        //
                        //                        var user2 = ucore.LoadEntity(o => o.U_Id == user.U_Id && o.U_State == 0);
                    }
                }

                if (rs.Code == 0) //满足投保要求
                {
                    string proconfig = System.IO.File.ReadAllText(Server.MapPath("/apisetting/" + pinfo.P_Code + ".txt"));
                    //当前投保产品的配置
                    dynamic config = JsonSerialize.Instance.JsonToObject(proconfig);

                    int okcount = 0;
                    List<Core_Order> succlist = new List<Core_Order>();//成功投保的订单

                    //                    StringBuilder tpls = new StringBuilder();
                    //投保人是否和被保人一致
                    bool flag = true;

                    if (!string.IsNullOrEmpty(obj.AppntName))
                    {
                        flag = false;
                    }

                    for (int i = 0; i < obj.Insured.Count; i++) //多个投保人
                    {
                        using (var client = new HttpClient())
                        {
                            string url = config.url;
                            client.BaseAddress = new Uri(url);
                            var requestJson = JsonConvert.SerializeObject(
                                new
                                {
                                    third_serial_no = DateTime.Now.ToString("yyyyMMddHHmmssfff"),
                                    user_name = config.userName,
                                    agent_code = config.userCode,
                                    //agent_com = "1234567890",
                                    //manage_com = "86110161",
                                    channel_detail = config.channelDetail,
                                    pol_apply_date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                    cvali_date = obj.CvaliDate,
                                    plan = new
                                    {
                                        plan_code = config.planCode,
                                        mult = obj.Insured[i].Mult
                                    },
                                    appnt = new
                                    {
                                        appnt_name = flag ? obj.Insured[i].Name : obj.AppntName,
                                        appnt_sex = flag ? (obj.Insured[i].Sex == "0" ? "F" : "M") : (obj.AppntSex == "0" ? "F" : "M"),
                                        appnt_birthday = flag ? obj.Insured[i].Birthday : obj.AppntBirthday,
                                        appnt_id_type = flag ? obj.Insured[i].IdType : obj.AppntIdType,
                                        appnt_id_no = flag ? obj.Insured[i].IdNo : obj.AppntIdNo
                                    },
                                    insured = new
                                    {
                                        relation_to_appnt = "31", //默认关系都是“其他 31”，具体参考文档关系码表
                                        insured_name = obj.Insured[i].Name,
                                        insured_sex = obj.Insured[i].Sex == "0" ? "F" : "M",
                                        insured_birthday = obj.Insured[i].Birthday,
                                        insured_id_type = obj.Insured[i].IdType,
                                        insured_id_no = obj.Insured[i].IdNo,
                                        //occupation_code = "1020202"

                                    }
                                });



                            HttpContent httpContent = new StringContent(requestJson);

                            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");



                            var result =
                                client.PostAsync("services/rest/policy/new", httpContent)
                                    .Result.Content.ReadAsStringAsync()
                                    .Result;

                            var clrs = JsonSerialize.Instance.JsonToObject(result);

                            Sys_OperateLog log = new Sys_OperateLog()
                            {
                                L_UId = user.U_Id.ToString(),
                                L_Phone = user.U_Phone,
                                L_Flag = 2,
                                L_Url = "order/policy",
                                L_Desc = requestJson,
                                L_Time = DateTime.Now,
                                L_Status = 0
                            };

                            if (clrs.result == "succ") // 成功下单了，通知改数据库
                            {
                                long curno = 0; //当前单证号码

                                //修改单证库存数量
                                if (user.U_Role == 0 && ticket != null) //超管
                                {
                                    curno = ticket.T_CurrentNo;

                                    ticket.T_CurrentNo += 1;
                                    ticket.T_RemainCount -= 1;

                                    tcore.UpdateEntity(ticket);

                                }
                                else
                                {
                                    curno = ticketHistory.H_CurrentNo;
                                    ticketHistory.H_CurrentNo += 1;
                                    ticketHistory.H_RemainCount -= 1;
                                    thcore.UpdateEntity(ticketHistory);
                                }

                                //修改用户发票/单证数量
                                user.U_InvoiceCount -= 1;
                                user.U_TicketCount -= 1;
                                ucore.UpdateEntity(user);


                                //添加投保记录
                                Core_Order order = new Core_Order()
                                {
                                    O_Id = Guid.NewGuid(),
                                    O_Count = Convert.ToInt32(obj.Insured[i].Mult),
                                    O_CreateTime = DateTime.Now,
                                    O_ProductName = pinfo.P_Name,
                                    O_ProductId = pinfo.P_Id,
                                    O_UserAvail = obj.Insured[i].BnfName,
                                    O_UserBirthday = obj.Insured[i].Birthday,
                                    O_UserCardNo = obj.Insured[i].IdNo,
                                    O_UserCardType = Convert.ToInt32(obj.Insured[i].IdType),
                                    O_UserName = obj.Insured[i].Name,
                                    O_UserPhone = obj.Insured[i].Phone,
                                    O_UserRelation = "其他",
                                    O_UserSex = Convert.ToInt32(obj.Insured[i].Sex),
                                    O_InsureBirthday = flag ? obj.Insured[i].Birthday : obj.AppntBirthday,
                                    O_InsureCardNo = flag ? obj.Insured[i].IdNo : obj.AppntIdNo,
                                    O_InsureCardType = Convert.ToInt32(flag ? obj.Insured[i].IdType : obj.AppntIdType),
                                    O_InsureName = flag ? obj.Insured[i].Name : obj.AppntName,
                                    O_InsureSex = Convert.ToInt32(flag ? obj.Insured[i].Sex : obj.AppntSex),
                                    O_OrderNo = clrs.third_serial_no,
                                    O_PolicyNo = clrs.policy_no,
                                    O_STime = Convert.ToDateTime(obj.CvaliDate),
                                    O_ETime = DateTime.Now.AddDays(pinfo.P_Timeout),
                                    O_TicketNo = curno,
                                    O_WorkerId = user.U_Id,
                                    O_WorkerName = user.U_Name,
                                    O_State = 0,
                                    O_FlightNo = obj.Flight,
                                    O_Price = pinfo.P_PolicyPrice,
                                    O_BackUId = 0,
                                    O_InsuredAmount = pinfo.P_InsuredAmount

                                };
                                ocore.AddEntity(order);
                                log.L_Status = 0;
                                okcount++;

                                succlist.Add(order);


                                rs.Msg = "成功投保：" + okcount + "份保险";
                                new OperateLogCore().AddEntity(log);
                            }
                            else
                            {
                                rs.Code = 1; //投保超量，接口不予投保
                                log.L_Desc = "###" + clrs + log.L_Desc;
                                new OperateLogCore().AddEntity(log);
                                rs.Msg = clrs.message;

                                return Json(rs, JsonRequestBehavior.AllowGet);
                            }

                        }
                    }
                    rs.Data = new
                    {
                        tpl = pinfo.P_PrintTemplate,  //打印模板
                        list = succlist  //订单列表内容
                    };
                    //rs.Data = okcount;
                }
            }
            else
            {
                rs.Msg = "产品不存在";
            }


            return Json(rs, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 退保，单号
        /// </summary>
        /// <param name="no">单号</param>
        /// <param name="flag">1保单号 ,2单证号</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult OuttimeBack(string no, int flag)
        {
            var user = GetUser();
            int okcount = 0;
            var rs = new ResultDataModel() { Msg = "保单不存在" };

            var arr = no.Split(',');
            foreach (var s in arr)
            {
                Core_Order order = null;

                if (flag == 1)
                {
                    order = ocore.LoadEntity(o => o.O_PolicyNo == s && o.O_State == 0);
                }
                else if (flag == 2)
                {
                    order = ocore.LoadEntity(o => o.O_OrderNo == s && o.O_State == 0);
                }
                if (order != null)
                {
                    var pinfo = pcore.LoadEntity(o => o.P_Id == order.O_ProductId);

                    using (var client = new HttpClient())
                    {
                        string proconfig = System.IO.File.ReadAllText(Server.MapPath("/apisetting/" + pinfo.P_Code + ".txt"));
                        //当前投保产品的配置
                        dynamic config = JsonSerialize.Instance.JsonToObject(proconfig);
                        string url = config.url;
                        client.BaseAddress = new Uri(url);
                        var requestJson = JsonConvert.SerializeObject(
                            new
                            {
                                third_serial_no = order.O_OrderNo,
                                user_name = "cecece1",
                                policy_no = order.O_PolicyNo,
                                cancel_date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                prem = ""
                            });

                        HttpContent httpContent = new StringContent(requestJson);

                        httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");



                        var result = client.PostAsync("services/rest/policy/cancel", httpContent).Result.Content.ReadAsStringAsync().Result;

                        var clrs = JsonSerialize.Instance.JsonToObject(result);

                        Sys_OperateLog log = new Sys_OperateLog()
                        {
                            L_UId = user.U_Id.ToString(),
                            L_Phone = user.U_Phone,
                            L_Flag = 2,
                            L_Url = "order/policy",
                            L_Desc = requestJson,
                            L_Time = DateTime.Now,
                            L_Status = -1 //撤销，操作失败
                        };

                        if (clrs.result == "succ") // 成功下单了，通知改数据库
                        {
                            //状态过期撤保
                            order.O_State = 2;
                            order.O_BackMemo = "";
                            order.O_BackTime = DateTime.Now;
                            order.O_BackUId = user.U_Id;
                            order.O_BackUName = user.U_Name;
                            //修改order
                            ocore.UpdateEntity(order);
                            log.L_Status = 0; ////撤销，操作成功
                            okcount++;

                            rs.Msg = "成功退保：" + okcount + "份保险";
                            new OperateLogCore().AddEntity(log);
                        }
                        else
                        {
                            rs.Code = 1; //撤销失败
                            log.L_Desc = "###" + clrs + log.L_Desc;
                            new OperateLogCore().AddEntity(log);
                            rs.Msg = clrs.message;
                        }
                    }
                }
            }

            return Json(rs, JsonRequestBehavior.AllowGet);

            //            Core_Order order = null;
            //
            //            if (flag == 1)
            //            {
            //                order = ocore.LoadEntity(o=>o.O_PolicyNo == no);
            //            }else if (flag == 2)
            //            {
            //                order = ocore.LoadEntity(o => o.O_OrderNo== no);
            //            }
            //            else
            //            {
            //                throw new Exception("数据不存在");
            //            }
            //            if (order== null)
            //            {
            //                throw new Exception("数据不存在");
            //            }
            //            if (order.O_State != 0)
            //            {
            //                throw new Exception("数据已退保");
            //
            //            }

        }

        /// <summary>
        /// 订单详细信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Info(Guid id)
        {
            var obj = ocore.LoadEntity(o => o.O_Id == id);

            if (GetUser().U_Role != 0 && obj != null && obj.O_WorkerId != GetUser().U_Id)
            {
                throw new Exception("您没权限查看");
            }
            return View(obj);
        }

        /// <summary>
        /// 数据统计
        /// </summary>
        /// <returns></returns>
        public ActionResult Tongji(int id = 1, int size = 10, string key = "")
        {
            Func<OrderIndexViewModel, bool> expr = t => true;
            var user = GetUser();
            string where = " 1=1 ";
            if (user.U_Role != 0)
            {
                where += $" and a.U_Id={user.U_Id} or a.U_PId = {user.U_Id}";
            }
            if (!string.IsNullOrWhiteSpace(key))
            {
                expr = t => t.U_Name.Contains(key.Trim());
            }
            string sql = @"select U_Id,U_Name,U_Role,U_InterfaceCount,U_InvoiceCount,U_TicketCount,ISNULL(b.invalid,0) as U_Invalid,ISNULL(b.valid,0) as U_Valid,ISNULL(b.price,0) as U_Price from core_user  a
                      left join (
                      select O_WorkerId,  SUM(CASE  WHEN O_State=0 THEN 1 ELSE 0 END) AS 'valid' ,
                      SUM(CASE  WHEN O_State=1 THEN 1 ELSE 0 END) AS 'invalid',
                      SUM(O_Price) AS 'price'
                      FROM Core_Order GROUP BY O_WorkerId
                      ) b
                      on a.U_Id = b.O_WorkerId
                      where " + where + " order by U_Price desc";

            using (lvzhiEntities db = new lvzhiEntities())
            {
                var list = db.Database.SqlQuery<OrderIndexViewModel>(sql).Where(expr).ToPagedList(id, size); ;
                return View(list);
            }

            return View();
        }
        /// <summary>
        /// 统计详单
        /// </summary>
        /// <returns></returns>
        public ActionResult TongjiDetail(int uid, int id = 1, int size = 10)
        {

            var list = ocore.LoadEntities(o => o.O_WorkerId == uid).OrderByDescending(o => o.O_CreateTime).ToPagedList(id, size);
            return View(list);
        }
        /// <summary>
        /// 打印
        /// </summary>
        /// <returns></returns>
        public ActionResult Print()
        {
            return View();
        }
        /// <summary>
        /// 退保
        /// </summary>
        /// <returns></returns>
        public ActionResult Back()
        {
            return View();
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public string Remove(Guid id)
        {
            //            if (GetUser().U_Role != 0)
            //            {
            //                throw new Exception("您没有权限");
            //            }
            var info = ocore.LoadEntity(o => o.O_Id == id && o.O_State == 0);

            if (info != null)
            {

                info.O_State = 1;

                if (ocore.UpdateEntity(info)) //更改状态
                {
                    return "ok";
                }

            }
            return "更新数据失败";
        }
        /// <summary>
        /// 查询单个订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult FindOne(Guid id)
        {
            var rs = new ResultDataModel();

            //            if (GetUser().U_Role != 0)
            //            {
            //                throw new Exception("您没有权限");
            //            }
            var info = ocore.LoadEntity(o => o.O_Id == id);


            if (info != null)
            {
                var p = pcore.LoadEntity(o => o.P_Id == info.O_ProductId);

                rs.Code = 0;
                rs.Data = new
                {
                    tpl = p.P_PrintTemplate,//模板
                    order = info //订单
                };
            }
            return Json(rs, JsonRequestBehavior.AllowGet);
        }

    }
}